<?php
namespace app\api\controller;
use app\common\model\Setup as SetupModel;
use app\common\model\ProjectClassify as ProjectClassifyModel;
use app\common\model\Project as ProjectModel;
use app\common\model\OrderSetup as OrderSetupModel;
use app\common\model\Order as OrderModel;
use app\common\model\AdminUser as AdminUserModel;

use think\Controller;
use think\Config;
use think\Db;
use think\Session;
use think\Cookie;

/**
 * 扫码核销
 * Class Order
 * @package app\api\controller
 */
class Order extends Controller
{
    protected $setup_model;
    protected $project_classify_model;
    protected $project_model;
    protected $order_setup_model;
    protected $order_model;
    protected $admin_user_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->setup_model                   = new SetupModel();
        $this->project_classify_model        = new ProjectClassifyModel();
        $this->project_model                 = new ProjectModel();
        $this->order_setup_model             = new OrderSetupModel();
        $this->order_model                   = new OrderModel();
        $this->admin_user_model              = new AdminUserModel();

        $setup   = $this->setup_model->find(1);
        $this->assign('setup', $setup);
    }

    /**
     * 扫码签到核销
     */
    public function code_sign($sid="",$pid="",$cleanup = "",$time = "",$id="")
    {
        $order_setup_list=[];
        $project=[];
        if($pid){
            $project=$this->project_model->find($pid);

            //定义查询
            if ($cleanup) {
                Session::delete('time');
            }
            if ($time) {
                Session::set('time',$time);
            }

            $where = [];
            $where['pid']  = $pid;

            if (Session::get('time')) {
                $where['time'] = Session::get('time');
            }else{
                $starttime=date("Y-m-d",time());
                $endtime=date("Y-m-d",strtotime("+1 week"));
                $where['time'] =['between', [$starttime, $endtime]];
            }

            $order_setup = $this->order_setup_model->where($where)->order(['id' => 'ASC'])->select();
            foreach($order_setup as $key => $value) {
                $time=date("Y-m-d",strtotime($value['time'])).' '.$value['end_time'];
                if (strtotime($time)>=time()) {
                    $value['time_status']=0;
                }else{
                    $value['time_status']=1;
                }
                //判断是否预约
                $order_see=$this->order_model->where(['sid'=>$value['id'],'uid'=>Session::get('index_id')])->find();
                if($order_see && Session::get('index_id')){
                    $value['order_status']=0;
                }else{
                    $value['order_status']=1;
                }
                //区域
                $value['project']=$this->project_model->find($value['pid']);
                $value['order_count']=$this->order_model->where(['sid'=>$value['id'],'status'=>['neq',2],'oid'=>['eq',null]])->count('number');
                $order_setup_list[]=$value;
            }
        }

        $order_setup_see = [];
        if($sid){
            $order_setup_see=$this->order_setup_model->find($sid);
            //预约项目
            $order_setup_see['project']=$this->project_model->find($order_setup_see['pid']);
            //预约分类
            $project_classify=$this->project_classify_model->find($order_setup_see['project']['cid']);
            $project_classify_path=explode(",",$project_classify['path']);
            $order_setup_see['project']['project_classify']="";
            foreach($project_classify_path as $kk => $vv) {
                if($vv!=0 && $vv!=null){
                    $project_classify_level=$this->project_classify_model->find($vv);
                    if($order_setup_see['project']['project_classify']){
                        $order_setup_see['project']['project_classify']=$order_setup_see['project']['project_classify'].'-'.$project_classify_level['title'];
                    }else{
                        $order_setup_see['project']['project_classify']=$project_classify_level['title'];
                    }
                }
            }
            if($order_setup_see['project']['project_classify']){
                $order_setup_see['project']['project_classify']=$order_setup_see['project']['project_classify'].'-'.$project_classify['title'];
            }else{
                $order_setup_see['project']['project_classify']=$project_classify['title'];
            }
            $order_setup_see['order_count']=$this->order_model->where(['sid'=>$order_setup_see['id'],'status'=>['neq',2],'oid'=>['eq',null]])->count('number');
        }

        return $this->fetch('code_sign', [
            'project' => $project,
            'order_setup_list' => $order_setup_list,
            'order_setup_see' => $order_setup_see,
            'pid'  => $pid,
            'id' => $id,
            'time' => Session::get('time'),
            'sid' => $sid
        ]);
    }

    /**
     * 扫码签到核销
     */
    public function code_save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            //判断预约权限
            if(!$data['tel'] || !$data['id_card']){
                $this->error('请先填写电话和身份证信息');
            }
            $data_order['sid']        = $data['sid'];
            $data_order['uid']        = Session::get('index_id');
            $data_order['oid']        = $data['oid'];
            $data_order['type']       = 1;
            $data_order['name']       = $data['name'];
            $data_order['tel']        = $data['tel'];
            $data_order['id_card']    = $data['id_card'];
            $data_order['number']     = $data['number'];
            $data_order['content']    = $data['content'];
            $data_order['status']     = 1;
            if ($this->order_model->allowField(true)->save($data_order)) {
                $order_setup_see=$this->order_setup_model->find($data['sid']);
                //发送预约提醒
                $project=$this->project_model->find($order_setup_see['pid']);
                $admin_user_list = $this->admin_user_model->order(['id' => 'DESC'])->select();
                foreach($admin_user_list as $k => $v) {
                    if($v['mobile']){
                        if($v['cid']==0 || $v['cid']==$project['cid']){
                            $Interfaces=new Interfaces();
                            $Interfaces->tel_remind($v['mobile'],$project['title']);
                        }
                    }
                }
                $this->success($this->order_model->id);
            } else {
                $this->error('预约核销失败');
            }
        }
    }

}